package kibua.fun.car;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Rennstrecke {
	
	JFrame frame = new JFrame();
	JPanel hauptPanel = new JPanel();
	Autovisualisierung visu1 = new Autovisualisierung(new Auto());
	
	public static void main(String[] args){
		Rennstrecke renn1 = new Rennstrecke();
		renn1.play();
	}
	
	public Rennstrecke() {
		frame.setLayout(new BorderLayout());
		frame.add(hauptPanel,BorderLayout.CENTER);
		hauptPanel.setLayout(new BorderLayout());
		hauptPanel.add(visu1,BorderLayout.CENTER);
		visu1.setPreferredSize(new Dimension(800,800));
		frame.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				if(arg0.getKeyCode()==KeyEvent.VK_LEFT){
					visu1.auto.lenkeLinks();
				}
				
				if(arg0.getKeyCode()==KeyEvent.VK_RIGHT){
					visu1.auto.lenkeRechts();
				}
				
			}
		});
		frame.pack();
		frame.setVisible(true);
		
	}
	
	public void play(){
		double alteZeit=System.currentTimeMillis();
		do{
			double aktuelleZeit = System.currentTimeMillis();
			visu1.auto.fahre(alteZeit-aktuelleZeit);
			 alteZeit= System.currentTimeMillis();
			try {
				visu1.updateUI();
				Thread.sleep(1000/60);
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}while(true);
	}

}
